package com.miguo.framework.web;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import com.miguo.framework.R;
import com.miguo.framework.app.AppFramework;
import com.miguo.framework.utils.Log;
import com.miguo.framework.web.callback.JsCallJava;
import com.miguo.framework.web.utils.WebUtils;

public class CustomWebChromeClient extends WebChromeClient {

    private IWebChromeClient mWebChromeClient;
    private Bitmap mDefaultVideoPoster = null;
    private View mVideoProgressView = null;
    private JsCallJava mJsCallJava;
    private boolean mIsInjectedJS;

    public CustomWebChromeClient(IWebChromeClient WebChromeClient) {
        super();
        mWebChromeClient = WebChromeClient;
    }

    public CustomWebChromeClient(IWebChromeClient WebChromeClient,
                                 String injectedName, Class injectedCls) {
        super();
        mWebChromeClient = WebChromeClient;
        mJsCallJava = new JsCallJava(injectedName, injectedCls);
    }

    public CustomWebChromeClient(IWebChromeClient WebChromeClient,
                                 JsCallJava jsCallJava) {
        super();
        mWebChromeClient = WebChromeClient;
        mJsCallJava = jsCallJava;
    }

    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        mWebChromeClient.openFileChooser();
    }

    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
        mWebChromeClient.openFileChooser();
    }

    @Override
    public Bitmap getDefaultVideoPoster() {
        if (mDefaultVideoPoster == null) {
            mDefaultVideoPoster = BitmapFactory.decodeResource(AppFramework
                    .getContext().getResources(), R.drawable.web_video_poster);
        }

        return mDefaultVideoPoster;
    }

    @Override
    public View getVideoLoadingProgressView() {
        if (mVideoProgressView == null) {
            LayoutInflater inflater = LayoutInflater.from(AppFramework
                    .getContext());
            mVideoProgressView = inflater.inflate(R.layout.web_video_loading_progress, null);
        }

        return mVideoProgressView;
    }

    public void onShowCustomView(View view,
                                 CustomViewCallback callback) {
        mWebChromeClient.onShowCustomView(view, callback);
    }

    @Override
    public void onHideCustomView() {
        mWebChromeClient.onHideCustomView();
    }

    @Override
    public void onProgressChanged(WebView view, int progress) {
        if (mJsCallJava != null) {
            if (progress <= 25) {
                mIsInjectedJS = false;
            } else if (!mIsInjectedJS) {
                if (!TextUtils.isEmpty(view.getUrl())) {
                    if (WebUtils.isMGUrl(view.getUrl())) {
                        view.loadUrl(mJsCallJava.getPreloadInterfaceJS());
                        Log.d("inject js interface completely on progress "
                                + progress);
                    } else {
                        Log.d("Request domain does not match the termination of the injection");
                    }
                    mIsInjectedJS = true;
                }
            }
        }

        if (progress >= 80) {
            mWebChromeClient.initSelected();
            mWebChromeClient.freshBtnState(false);
        }
        mWebChromeClient.onProgressChanged(view, progress);
        ((CustomWebView) view).setProgress(progress);
    }

    // 获取网站图标
    @Override
    public void onReceivedIcon(WebView view, Bitmap icon) {
        super.onReceivedIcon(view, icon);
    }

    @Override
    public boolean onCreateWindow(WebView view, boolean dialog,
                                  boolean userGesture, Message resultMsg) {
        mWebChromeClient.onCreateWindow(view, dialog, userGesture, resultMsg);
        resultMsg.sendToTarget();
        return true;
    }

    // 获取、设置标题
    @Override
    public void onReceivedTitle(WebView view, String title) {
        mWebChromeClient.onReceivedTitle(view, title);
        super.onReceivedTitle(view, title);
    }

    @Override
    public boolean onJsAlert(WebView view, String url, String message,
                             final JsResult result) {
        result.confirm();
        return true;
    }

    @Override
    public boolean onJsConfirm(WebView view, String url, String message,
                               final JsResult result) {
        result.confirm();
        return true;
    }

    @Override
    public boolean onJsPrompt(WebView view, String url, String message,
                              String defaultValue, final JsPromptResult result) {
        if (mJsCallJava != null) {
            result.confirm(mJsCallJava.call(view, message));
        } else {
            result.confirm(message);
        }
        return true;
    }

}
